<template>
	<el-container>
		<el-aside width="50%">
			<el-container style="display:unset">
				<el-card shadow="never" header="审批人员设置">
					<scTable ref="table1" :data="noticeTypeList" row-key="value">
						<el-table-column label="#" width="80">
							<template #default="scope">
								{{ scope.$index + 1 }}
							</template>
						</el-table-column>
						<el-table-column label="公告分类" prop="name" width="180"></el-table-column>
						<el-table-column label="创建时间" prop="createTime" width="180"></el-table-column>
						<el-table-column label="审批人" prop="approver" fixed="right" align="right" width="300">
							<template #default="scope">
								{{scope.row.approverName}}
								<el-button-group>
									<el-button text type="primary" size="small" @click="setApprover(scope.row, scope.$index)">设置</el-button>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-card>
			</el-container>
			<el-container style="display:unset">
				<el-card shadow="never" header="免审人员设置">
					<scTable ref="table2" :data="noticeTypeList" row-key="value">
						<el-table-column label="#" width="80">
							<template #default="scope">
								{{ scope.$index + 1 }}
							</template>
						</el-table-column>
						<el-table-column label="公告分类" prop="name" width="180"></el-table-column>
						<el-table-column label="创建时间" prop="createTime" width="180"></el-table-column>
						<el-table-column label="审批人" prop="notApprover" fixed="right" align="right" width="300">
							<template #default="scope">
								{{scope.row.notApproverName}}
								<el-button-group>
									<el-button text type="primary" size="small" @click="setNotApprover(scope.row, scope.$index)">设置</el-button>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-card>
			</el-container>
		</el-aside>
		<el-container>
			<el-card shadow="never" header="模版设置">
				<el-header>
					<div class="left-panel">
						<el-button type="primary" icon="el-icon-plus" @click="addNoticeTemplate"></el-button>
						<el-button type="danger" plain icon="el-icon-delete" @click="delNoticeTemplate"></el-button>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj" row-key="templateId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
						<el-table-column type="selection" width="50"></el-table-column>
						<el-table-column label="#" type="index" width="50"></el-table-column>
						<el-table-column label="模版名称" prop="templateName" width="180"></el-table-column>
						<el-table-column label="模版文件" prop="attachId" width="180">
							<template #default="scope">
								<attach-menu :attachIds="scope.row.attachId" :roleFlag="1" :preview="true"></attach-menu>
							</template>
						</el-table-column>
						<el-table-column label="通知分类" prop="noticeTypeName" width="180"></el-table-column>
						<el-table-column label="状态" prop="status" width="80">
							<template #default="scope">
								<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
							</template>
						</el-table-column>
						<el-table-column label="创建人" prop="createUserName" width="150"></el-table-column>
						<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
						<el-table-column label="备注说明" prop="remark" width="150"></el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="170">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
									<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
			</el-card>
		</el-container>
	</el-container>
	<select-user-dialog v-model:accountId="form.approver" ref="selectUserDialog" :is-not-show-btn="true" :mode="false" @success="updateConfig"/>
	<select-user-dialog v-model:accountId="form.notApprover" ref="selectNotUserDialog" :is-not-show-btn="true" :mode="false" @success="updateConfig"/>
	<templatedialog v-if="templateDialog.save" ref="templateDialog" @success="saveTemplateSuccess" @closed="templateDialog.save=false"></templatedialog>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import Templatedialog from "@/views/info/notice/setting/templatedialog";
import selectUserDialog from "@/views/module/selectUserDialog";
export default {
	name: 'Notice Setting',
	components: {Templatedialog,AttachMenu,selectUserDialog},
	data() {
		return {
			form:{
				noticeType:"",
				approver:"",
				notApprover:"",
			},
			noticeTypeList: [],
			templateDialog: {
				save: false,
			},
			apiObj: this.$API.info.notice.getNoticeTemplateList
		}
	},
	mounted() {
		this.getNoticeConfigList();
	},
	methods: {
		setApprover(row,index)
		{
			this.form.noticeType = row.value;
			this.form.approver=row.approver;
			this.$nextTick(() => {
				this.$refs.selectUserDialog.selectOrgUser();
			})
		},
		setNotApprover(row,index)
		{
			this.form.noticeType = row.value;
			this.form.notApprover=row.notApprover;
			this.$nextTick(() => {
				this.$refs.selectNotUserDialog.selectOrgUser();
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		addNoticeTemplate() {
			this.templateDialog.save = true
			this.$nextTick(() => {
				this.$refs.templateDialog.open("add")
			})
		},
		delNoticeTemplate() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var templateIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					templateIdArr.push(this.selection[i].templateId);
				}
				var res = await this.$API.info.notice.deleteNoticeTemplateByIds.post({ids:templateIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		table_show(row, index) {
			this.templateDialog.save = true
			this.$nextTick(() => {
				this.$refs.templateDialog.open('show').setData(row)
			})
		},
		table_edit(row, index) {
			this.templateDialog.save = true
			this.$nextTick(() => {
				this.$refs.templateDialog.open('edit').setData(row)
			})
		},
		async table_del(row, index) {
			var reqData = {templateId: row.templateId}
			var res = await this.$API.info.notice.deleteNoticeTemplate.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeSwitch(val, row) {
			row.status = row.status == '1' ? '0' : '1'
			row.$switch_status = true;
			delete row.$switch_status;
			row.status = val;
			var res = await this.$API.info.notice.updateNoticeTemplate.post({templateId: row.templateId, status: row.status});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getNoticeConfigList()
		{
			var res = await this.$API.info.notice.getNoticeConfigList.get();
			if (res.code == 200) {
				this.noticeTypeList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async updateConfig(){
			var res = await this.$API.info.notice.updateNoticeConfig.post(this.form);
			if (res.code == 200) {
				this.getNoticeConfigList();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		saveTemplateSuccess()
		{
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
